package com.souku.finance.application.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.UsingExpBean;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.CircleViewExp;
import com.souku.finance.application.widget.MyTextView;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by Android on 2016/12/21.
 */

public class ExperienceActivity extends BaseActivity implements View.OnClickListener {

    protected CircleViewExp circle;
    protected TextView tvCircle;
    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected ImageView topMore;
    protected TextView topTvMore;
    protected MyTextView tvMoneytop;
    protected MyTextView tvMoney;
    protected Button btnOk;
    protected MyTextView tvPoint;
    protected MyTextView tvDay;
    protected TextView tvName;
    protected TextView tvDate;
    protected MyTextView tvWithdrawmoney;
    protected Button btnWithdraw;
    private UsingExpBean bean;
    private ImageView ivCircle;

    @Override
    public void internet() {
        getUsing_exp_money();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_experience);
        initView();
        circle.setStoke(this);
        topTitle.setText("体验金");
        topMore.setVisibility(View.VISIBLE);
        getUsing_exp_money();
    }

    private void initView() {
        circle = (CircleViewExp) findViewById(R.id.circle);
        tvCircle = (TextView) findViewById(R.id.tv_circle);
        MyTextUtil.setType(this, tvCircle);
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(ExperienceActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        topMore = (ImageView) findViewById(R.id.top_more);
        topMore.setImageResource(R.drawable.exp_list);
        topMore.setOnClickListener(ExperienceActivity.this);
        topTvMore = (TextView) findViewById(R.id.top_tv_more);
        tvMoneytop = (MyTextView) findViewById(R.id.tv_moneytop);
        tvMoney = (MyTextView) findViewById(R.id.tv_money);
        btnOk = (Button) findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(ExperienceActivity.this);
        tvPoint = (MyTextView) findViewById(R.id.tv_point);
        tvDay = (MyTextView) findViewById(R.id.tv_day);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvDate = (TextView) findViewById(R.id.tv_date);
        tvWithdrawmoney = (MyTextView) findViewById(R.id.tv_withdrawmoney);
        btnWithdraw = (Button) findViewById(R.id.btn_withdraw);
        ivCircle = (ImageView) findViewById(R.id.iv_circle);
        btnWithdraw.setOnClickListener(ExperienceActivity.this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        } else if (view.getId() == R.id.top_more) {
            Intent intent = new Intent(this, ExperienceListActivity.class);
            startActivity(intent);
        } else if (view.getId() == R.id.btn_ok) {
            use_exp_money();
        } else if (view.getId() == R.id.btn_withdraw) {
            withdraw_exp_interest();
        }
    }

    private void getUsing_exp_money() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("userId", ShareUtil.getInstance().getString(Constant.UserId));
        RequestManager.getInstance().post(UrlConstact.USING_EXP_MONEY, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    bean = JsonUtil.fastBean(response, UsingExpBean.class);
                    setDate();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
//                is_new_exp_money();
            }
        }), ParamUtil.getHeaders());
    }

    private void is_new_exp_money() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("userId", ShareUtil.getInstance().getString(Constant.UserId));
        RequestManager.getInstance().post(UrlConstact.NEW_EXP_MONEY, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    boolean b = JsonUtil.getBoolean(JsonUtil.getJSONObject(response), "data");
                    setIcon(b);
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void use_exp_money() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("emid", bean.getData().get(0).getId());
        RequestManager.getInstance().post(UrlConstact.USE_EXP_MONEY, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    ToastUtil.show("使用成功");
                    getUsing_exp_money();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void withdraw_exp_interest() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("emid", bean.getData().get(0).getId());
        RequestManager.getInstance().post(UrlConstact.WITHDRAW_EXP_INTEREST, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                ToastUtil.show(JsonUtil.getMessage(response));
                if (JsonUtil.getCode(response) == 1) {
                    MineFragment.isChange = true;
                    getUsing_exp_money();
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void setIcon(boolean b) {
        if (b) {
            topMore.setImageResource(R.drawable.exp_list);
        } else {
            topMore.setImageResource(R.drawable.exp_list);
        }
    }

    private void setDate() {
        if (bean == null || bean.getData().size() == 0) {
            return;
        }
        tvMoney.setText(MyTextUtil.setMoneyToElement(bean.getData().get(0).getMoney()));
        tvPoint.setText(bean.getData().get(0).getRate() + "%");
        tvDay.setText(bean.getData().get(0).getDuration());
        tvDate.setText(MyTextUtil.changeTimeFormat2(bean.getData().get(0).getGmtCreated()));
        tvName.setText(bean.getData().get(0).getName());
        if (bean.getData().get(0).getState().equals("1")) {
            tvMoneytop.setText("0.0");
            tvCircle.setText("0.0");
            circle.setPrograss(0.0, 100, tvWithdrawmoney);
            btnOk.setText("使用");
            btnOk.setBackgroundResource(R.drawable.solid_red_round);
            btnOk.setEnabled(true);
            btnWithdraw.setBackgroundResource(R.drawable.solid_gray_round);
            btnWithdraw.setEnabled(false);
        } else {
            tvMoneytop.setText(MyTextUtil.setMoneyToElement(bean.getData().get(0).getMoney()));
            tvCircle.setText(MyTextUtil.setMoneyToElement(bean.getData().get(0).getExpectedInterest()));
            circle.setPrograss(Integer.parseInt(bean.getData().get(0).getCurrentInterest())
                    , Integer.parseInt(bean.getData().get(0).getExpectedInterest()), tvWithdrawmoney);
            btnOk.setEnabled(false);
            if (bean.getData().get(0).getState().equals("2")) {
                btnOk.setText("使用中");
                btnOk.setBackgroundResource(R.drawable.solid_main_round);
            } else if (bean.getData().get(0).getState().equals("3")) {
                btnOk.setText("已使用");
                btnOk.setBackgroundResource(R.drawable.solid_gray_round);
            }
            if (Integer.parseInt(bean.getData().get(0).getCurrentInterest()) == 0) {
                btnWithdraw.setBackgroundResource(R.drawable.solid_gray_round);
                btnWithdraw.setEnabled(false);
            } else {
                btnWithdraw.setBackgroundResource(R.drawable.solid_main_round);
                btnWithdraw.setEnabled(true);
            }
        }
    }
}
